package system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oracle.adf.view.rich.model.MDSMenuModel;

import org.apache.myfaces.trinidadinternal.menu.ImmutableGroupNode;
import org.apache.myfaces.trinidadinternal.menu.ImmutableItemNode;
import org.apache.myfaces.trinidad.model.ChildPropertyTreeModel;
import org.apache.myfaces.trinidadinternal.menu.MenuNode;

import view.common.Utils;

public class MenuBean extends MDSMenuModel {

    @Override
    public void setSource(String string) {
        super.setSource(string);
        ChildPropertyTreeModel treeModel =
            (ChildPropertyTreeModel)getWrappedData();
        List<MenuNode> listRoot = (List)treeModel.getWrappedData();
        if (treeModel != null) {
            //Xóa các biến pageFlowScope
            Utils.setPageFlowScope("whereclause", null);
            //Kết thúc xóa biến
            Map<String, MenuNode> mapNode = new HashMap();
            List<MenuNode> listProcess = listRoot;
            while (listProcess != null && listProcess.size() > 0) {
                List<MenuNode> listNext = new ArrayList();
                List<MenuNode> listDelete = new ArrayList();
                for (MenuNode obj : listProcess) {
                    if (obj instanceof ImmutableGroupNode) {
                        ImmutableGroupNode groupNode = (ImmutableGroupNode)obj;
                        if (groupNode != null) {
                            if (hasPermission(groupNode.getId(),
                                              groupNode.getIdRef()) == false) {
                                listDelete.add(groupNode);
                            } else {
                                for (MenuNode child :
                                     groupNode.getChildren()) {
                                    mapNode.put(child.getId(), groupNode);
                                    listNext.add(child);
                                }
                            }
                        }
                    }
                }
                for (MenuNode obj : listDelete) {
                    MenuNode loop = obj;
                    MenuNode loopParent = mapNode.get(obj.getId());
                    while (loopParent != null) {
                        loopParent.getChildren().remove(loop);
                        if (loopParent.getChildren().size() == 0) {
                            loop = loopParent;
                            loopParent = mapNode.get(loop.getId());
                        } else {
                            if (loopParent instanceof ImmutableGroupNode) {
                                ImmutableGroupNode groupNode =
                                    (ImmutableGroupNode)obj;
                                if (groupNode.getIdRef().equals(loop.getId())) {
                                    groupNode.setIdRef(groupNode.getChildren().get(0).getId());
                                }
                            }
                            loopParent = null;
                        }
                    }
                    if (listRoot.contains(loop)) {
                        listRoot.remove(loop);
                    }
                }
                listProcess = listNext;
            }
        }
    }

    private boolean hasPermission(String groupNodeId, String groupDefId) {
        SystemSecurity objSecurity = new SystemSecurity();
        GetPageDef pageDef = new GetPageDef();
        String strPageDef = pageDef.getPageDef(groupNodeId, groupDefId);
        if (strPageDef != null) {
            return objSecurity.checkPermission(strPageDef, "VIEW");
        }
        return false;
    }
}
